package com.zxl.yjb.activity.home.order;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zxl.yjb.R;
import com.zxl.yjb.adapter.MyFragmentPagerAdapter;
import com.zxl.yjb.adapter.SkillTabAdapter;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.SkillClassifyBean;
import com.zxl.yjb.fragment.home.SkillFragment;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.widget.TabView;

public class ServiceSelectActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_page_title)
    TextView tv_page_title;
    @BindView(R.id.tv_skill_classify)
    VerticalTabLayout tv_skill_classify;
    @BindView(R.id.vp_skill)
    NoScrollViewPager vp_skill;


    private ArrayList<Fragment> fragmentList = new ArrayList<>();
    private MyFragmentPagerAdapter skillPagerAdapter;

    private ArrayList<SkillClassifyBean> skillClassifyBeans = new ArrayList<>();
    private SkillTabAdapter skillTabAdapter;

    private DataManager dataManager;

    private String selectid = "";
    private String origin = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_select);
        ButterKnife.bind(this);
        selectid = getIntent().getStringExtra("id");
        origin = getIntent().getStringExtra("origin");
        initView();
    }

    @Override
    protected void initView() {
        tv_page_title.setText("选择服务类型");
        skillPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        vp_skill.setOffscreenPageLimit(4);
        vp_skill.setAdapter(skillPagerAdapter);
        tv_skill_classify.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                vp_skill.setCurrentItem(position, false);
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });

        getSkill();

    }


    private void getSkill() {
        dataManager = new DataManager();
        ObserverOnNextListener<List<SkillClassifyBean>> listener = new ObserverOnNextListener<List<SkillClassifyBean>>() {
            @Override
            public void onNext(List<SkillClassifyBean> skillClassifyBeanList) {
                skillClassifyBeans.clear();
                skillClassifyBeans.addAll(skillClassifyBeanList);
                fragmentList.clear();
                for (int i = 0; i < skillClassifyBeanList.size(); i++) {
                    SkillFragment skillFragment = new SkillFragment();
                    skillFragment.setData(skillClassifyBeans.get(i).getSub());
                    if (TextUtils.isEmpty(origin))
                        skillFragment.setOrigin(ServiceSelectActivity.class.getName());
                    else
                        skillFragment.setOrigin(origin);
                    fragmentList.add(skillFragment);
                }
                skillTabAdapter = new SkillTabAdapter(skillClassifyBeans);
                tv_skill_classify.setTabAdapter(skillTabAdapter);
                skillPagerAdapter.notifyDataSetChanged();

                if (!TextUtils.isEmpty(selectid))
                {
                    for (int i = 0; i < skillClassifyBeanList.size(); i++) {
                        if (selectid.equals(skillClassifyBeanList.get(i).getId()))
                        {
                            tv_skill_classify.setTabSelected(i);
                            vp_skill.setCurrentItem(i, false);
                        }
                    }
                }
            }
        };

        dataManager.getclassify(new ProgressObserver<ArrayList<SkillClassifyBean>>(listener, this), new HashMap<String, String>());
    }

    @OnClick({R.id.iv_back})
    void onClick(View view)
    {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;

            default:
                break;
        }
    }

}
